package com.bjgzy.rating.mvp.presenter;

import android.app.Application;

import com.bjgzy.rating.mvp.adapter.ExpandableItemAdapter;
import com.bjgzy.rating.mvp.contract.SignUpContract;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.SaveWorksData;
import com.eduhzy.ttw.commonsdk.entity.SignUp1ParentData;
import com.eduhzy.ttw.commonsdk.entity.SignUp2Data;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-8-9 11:43:59
 */
@ActivityScope
public class SignUpPresenter extends BasePresenter<SignUpContract.Model, SignUpContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    ExpandableItemAdapter mAdapter;

    @Inject
    public SignUpPresenter(SignUpContract.Model model, SignUpContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void saveWorks(SaveWorksData saveData) {
        mModel.saveWorks(saveData)
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<SignUp2Data>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<SignUp2Data> commonData) {
                        if (commonData.isResult()) {
                            mRootView.update(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }

    public void getClassify(int researchId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("researchId", researchId);
        mModel.getClassify(map)
                .retryWhen(new RetryWithDelay(2, 3))
                .compose(RxUtil.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<List<SignUp1ParentData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<SignUp1ParentData>> commonData) {
                        if (commonData.isResult()) {
                            mAdapter.getData().clear();
                            mAdapter.getData().addAll(commonData.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });
    }
}
